{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"KMP_DUPLICATE_LIB_OK\"]=\"TRUE\"\n",
    "from db.datasets import datasets\n",
    "from config import system_configs\n",
    "import json, os\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "from sklearn.metrics import average_precision_score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Label file: data/clsdata(1031)/cls/annotations/chart_val.json\n",
      "Loading from cache file: data/clsdata(1031)/cache/chart_val.pkl\n",
      "Loading annotations into memory...\n",
      "Done (t=0.14s)\n"
     ]
    }
   ],
   "source": [
    "with open('config/KPGrouping.json', \"r\") as f:\n",
    "    configs = json.load(f)\n",
    "    \n",
    "split = 'valchart'\n",
    "\n",
    "configs[\"system\"][\"data_dir\"] = \"/root/autodl-tmp/extraction_data\"\n",
    "configs[\"system\"][\"cache_dir\"] = \"data/cache/\"\n",
    "\n",
    "configs[\"system\"][\"dataset\"] =  \"Chart\"\n",
    "configs[\"system\"][\"snapshot_name\"] = \"PretrainKP\"\n",
    "system_configs.update_config(configs[\"system\"])\n",
    "db = datasets[\"Chart\"](configs[\"db\"], split)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_pie_center(a, b, c):\n",
    "    a,b,c = np.array(a), np.array(b), np.array(c)\n",
    "    ca = c - a\n",
    "    cb = c - b\n",
    "    # 计算向量 ca 和 cb 之间的角度的余弦值。\n",
    "    cosine_angle = np.dot(ca, cb) / (np.linalg.norm(ca) * np.linalg.norm(cb))\n",
    "    angle = np.arccos(cosine_angle)\n",
    "    r_square = (ca**2).sum()\n",
    "   # 判断向量 ca 和 cb 的叉积的符号。这一步用于确定扇形是在向量 ca 和 cb 之间还是在向量 ca 和 cb 之外。 \n",
    "    if ca[0]*cb[1]-ca[1]*cb[0] >= 0:\n",
    "        # 如果叉积大于或等于 0，则返回扇形的几何中心和面积（这里假设扇形是在向量 ca 和 cb 之间）。\n",
    "        return (a[0]+b[0]+c[0])/3., (a[1]+b[1]+c[1])/3., 0.5 * angle * r_square\n",
    "    else:\n",
    "        # 否则，返回扇形外侧的几何中心和面积。\n",
    "        return 2*c[0]-(a[0]+b[0]+c[0])/3., 2*c[1]-(a[1]+b[1]+c[1])/3., np.pi * r_square - 0.5 * angle * r_square\n",
    "\n",
    "def get_grouped_points(gts, preds, chartType):\n",
    "    gt_groups = []\n",
    "    area = 0\n",
    "        \n",
    "    if chartType == 'vbar_categorical':\n",
    "        # 遍历所有 ground truth 边界框。\n",
    "        for bbox in gts:\n",
    "            # 计算边界框的面积。\n",
    "            area = np.abs((bbox[2] - bbox[0]) * (bbox[3] - bbox[1])) \n",
    "            # 如果面积为 0，则跳过这个边界框。\n",
    "            if area == 0: continue\n",
    "            # 将非零面积的 ground truth 添加到 gt_groups。\n",
    "            gt_groups.append((bbox[:-1].reshape(-1, 2), area))\n",
    "    elif chartType == 'pie':\n",
    "        for bbox in gts:\n",
    "            # 处理逻辑与条形图类似，但是使用了 get_pie_center 函数来计算饼图分片的面积。\n",
    "            a, b, c = (bbox[0], bbox[1]), (bbox[2], bbox[3]), (bbox[4], bbox[5])\n",
    "            _, _, area = get_pie_center(a,b,c)        \n",
    "            if area == 0: continue\n",
    "            gt_groups.append((bbox[:-1].reshape(-1, 2), area))\n",
    "    elif chartType == 'line':\n",
    "        for bbox in gts:\n",
    "            # 遍历 ground truth，并使用特定的计算方式来获取面积。\n",
    "            detection = np.array(bbox)\n",
    "            if len(detection) <= 1: continue\n",
    "            assert len(detection) % 2 == 0\n",
    "            \n",
    "            xs = detection[0:len(detection):2]\n",
    "            ys = detection[1:len(detection):2]\n",
    "            area = (max(max(xs) - min(xs), max(ys) - min(ys)) / len(detection) * 2) ** 2\n",
    "            if area == 0: continue\n",
    "                \n",
    "            gt_groups.append((bbox.reshape(-1, 2), area))   \n",
    "\n",
    "    pred_groups = []\n",
    "    if '1' not in preds[0]: # baseline predictions\n",
    "        if chartType == 'pie':\n",
    "            for pred in preds:\n",
    "                pred_groups.append(np.array(pred[:-1]))\n",
    "        elif chartType == 'line':\n",
    "            for pred in preds:\n",
    "                pred_groups.append(np.array(pred))\n",
    "        else:\n",
    "            for pred in preds:\n",
    "                pred_groups.append(np.array(pred).reshape(-1, 2))\n",
    "    else:\n",
    "        for pred in preds[2]:\n",
    "            pred_groups.append(np.array(pred[2:-1]).reshape(-1, 2))\n",
    "    return gt_groups, pred_groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算 Object Keypoint Similarity（物体关键点相似度）。这是一种常用于评估物体检测和关键点估计任务性能的指标。\n",
    "def OKS(gt_p, pred_p, area):\n",
    "    # 计算了两个二维点（gt_p 为 ground truth 点，pred_p 为预测点）之间的欧氏距离的平方。\n",
    "    d2 = (gt_p[0] - pred_p[0]) ** 2 + (gt_p[1] - pred_p[1]) ** 2\n",
    "    # 一个常数，用于调整距离的权重。这个值可以根据具体应用进行调整。\n",
    "    k2 = 0.1\n",
    "    # 将函数输入的 area 直接赋值给 s2，表示物体或特征的面积。面积越大，对距离的容忍度越高。\n",
    "    s2 = area\n",
    "    #  OKS 的计算公式。函数返回一个介于 0 和 1 之间的值，用于表示 gt_p 和 pred_p 之间的相似度。值越接近 1，表示两点越相似。\n",
    "    return np.exp(d2/(s2 * k2) * (-1))\n",
    "\n",
    "# 计算一组物体关键点（gt_ps 为 ground truth 关键点，pred_ps 为预测关键点）之间的平均 Object Keypoint Similarity（OKS）。\n",
    "def GroupOKS(gt_ps, pred_ps):\n",
    "    # 初始化一个空列表，用于存储每个预测点与所有 ground truth 点之间的最大 OKS。\n",
    "    group_score = []\n",
    "    for pred_p in pred_ps:\n",
    "        # 初始化 max_score 为 0，用于存储当前预测点与所有 ground truth 点之间的最大 OKS。\n",
    "        max_score = 0.\n",
    "        for gt_p in gt_ps[0]:\n",
    "            max_score = max(max_score, OKS(gt_p, pred_p, gt_ps[1]))\n",
    "        group_score.append(max_score)\n",
    "        # 计算 group_score 列表中所有 OKS 的平均值，并返回。\n",
    "    return sum(group_score)/len(group_score)\n",
    "\n",
    "def computePrecision(gt_groups, pred_groups, thres=0.75):\n",
    "    count = 0\n",
    "    for pred_ps in pred_groups:\n",
    "        for gt_ps in gt_groups:\n",
    "            if GroupOKS(gt_ps, pred_ps) > thres:\n",
    "                count += 1\n",
    "                break\n",
    "\n",
    "    return count / len(pred_groups)\n",
    "\n",
    "def computeRecall(gt_groups, pred_groups, thres=0.75):\n",
    "    count = 0\n",
    "    for gt_ps in gt_groups:\n",
    "        for pred_ps in pred_groups:\n",
    "            if GroupOKS(gt_ps, pred_ps) > thres:\n",
    "                count += 1\n",
    "                break\n",
    "\n",
    "    return count / len(gt_groups)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('evaluation/KPGrouping5000.json') as f:\n",
    "    prediction = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 2/3695 [00:00<00:02, 1292.34it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'1': [[0.5221433639526367, 1.0, 66.86555480957031, 144.81338500976562], [0.5090314149856567, 1.0, 335.46820068359375, 166.95306396484375], [0.5082980394363403, 1.0, 335.1124572753906, 154.9727325439453], [0.5068081617355347, 1.0, 513.445556640625, 187.90850830078125], [0.5018704533576965, 1.0, 424.00164794921875, 193.54042053222656], [0.4925021529197693, 1.0, 666.405517578125, 224.15037536621094], [0.4898742139339447, 1.0, 398.6473693847656, 167.15406799316406], [0.48546725511550903, 1.0, 602.8373413085938, 223.72422790527344], [0.4747300446033478, 1.0, 513.415283203125, 198.84178161621094], [0.474124550819397, 1.0, 156.06536865234375, 113.29442596435547], [0.4688337445259094, 1.0, 602.7530517578125, 213.71917724609375], [0.46808141469955444, 1.0, 424.0223388671875, 164.97418212890625], [0.4615509808063507, 1.0, 155.94058227539062, 205.1022491455078], [0.4575537443161011, 1.0, 132.11447143554688, 144.93312072753906], [0.4562157988548279, 1.0, 67.15483856201172, 96.97804260253906], [0.4527580142021179, 1.0, 577.7569580078125, 199.3304443359375], [0.45181038975715637, 1.0, 310.3255615234375, 147.0564727783203], [0.44705358147621155, 1.0, 220.47186279296875, 205.04843139648438], [0.43939316272735596, 1.0, 691.43115234375, 247.60707092285156], [0.4368329644203186, 1.0, 246.08944702148438, 117.5040512084961], [0.42508453130722046, 1.0, 755.8212280273438, 248.05853271484375], [0.4232671558856964, 1.0, 245.86428833007812, 147.0049591064453], [0.41800644993782043, 1.0, 131.52432250976562, 303.9818420410156], [0.4136463701725006, 1.0, 488.1083068847656, 193.50424194335938], [0.4100768268108368, 1.0, 398.13934326171875, 304.26806640625], [0.4045097231864929, 1.0, 933.8985595703125, 304.0359802246094], [0.4003562927246094, 1.0, 665.8939208984375, 304.3281555175781], [0.3980456292629242, 1.0, 780.6403198242188, 256.9169921875], [0.3951363265514374, 1.0, 691.4340209960938, 229.71414184570312], [0.3914497196674347, 1.0, 577.3228149414062, 304.25067138671875], [0.38133543729782104, 1.0, 487.5596923828125, 304.2079162597656], [0.3812853991985321, 1.0, 219.6916961669922, 304.3770446777344], [0.3802695572376251, 1.0, 755.3552856445312, 304.1949462890625], [0.37856703996658325, 1.0, 869.4149169921875, 264.6464538574219], [0.35709241032600403, 1.0, 309.6137390136719, 304.14788818359375], [0.35457977652549744, 1.0, 845.0585327148438, 304.1690673828125], [0.3496888279914856, 1.0, 781.1469116210938, 262.89227294921875], [0.3153729736804962, 1.0, 845.1722412109375, 263.1808166503906], [0.21469052135944366, 1.0, 67.33297729492188, 304.43231201171875], [0.2059819996356964, 1.0, 602.653564453125, 304.4159240722656], [0.19789542257785797, 1.0, 155.82632446289062, 304.51171875], [0.169311061501503, 1.0, 691.2249755859375, 304.3822937011719], [0.16418056190013885, 1.0, 423.54351806640625, 304.4742431640625], [0.160277858376503, 1.0, 513.60400390625, 304.2743225097656], [0.15529930591583252, 1.0, 334.9799499511719, 304.1672058105469], [0.15267181396484375, 1.0, 245.89633178710938, 304.3756408691406], [0.15185008943080902, 1.0, 869.9356079101562, 304.13720703125], [0.13478527963161469, 1.0, 781.3704223632812, 304.1817626953125], [0.13082148134708405, 1.0, 398.4023132324219, 155.33140563964844], [0.1268686056137085, 1.0, 131.63890075683594, 96.77803802490234], [0.10253182053565979, 1.0, 933.7340698242188, 267.56781005859375], [0.09531237185001373, 1.0, 577.7920532226562, 188.30343627929688], [0.08336199820041656, 1.0, 946.2589721679688, 307.4576721191406], [0.08174736797809601, 1.0, 754.9837646484375, 231.46441650390625]], '2': [[0.10051372647285461, 2.0, 246.08944702148438, 117.5040512084961], [0.08040076494216919, 2.0, 67.15483856201172, 96.97804260253906], [0.0773557797074318, 2.0, 801.3771362304688, 25.58776092529297], [0.0772828459739685, 2.0, 382.5024108886719, 168.18186950683594]], '3': [[0.194779634475708, 3.0, 246.08944702148438, 117.5040512084961], [0.18156856298446655, 3.0, 756.08251953125, 248.3328857421875], [0.16435739398002625, 3.0, 156.06536865234375, 113.29442596435547], [0.14308418333530426, 3.0, 245.6199188232422, 146.33135986328125], [0.13896843791007996, 3.0, 488.5741882324219, 193.6169891357422], [0.11081433296203613, 3.0, 310.3255615234375, 147.0564727783203], [0.10973157733678818, 3.0, 220.65965270996094, 205.21762084960938], [0.10874401032924652, 3.0, 577.7569580078125, 199.3304443359375], [0.10791058838367462, 3.0, 155.94058227539062, 205.1022491455078], [0.10194903612136841, 3.0, 666.405517578125, 224.15037536621094], [0.1008274108171463, 3.0, 219.8121795654297, 112.97488403320312], [0.09658998250961304, 3.0, 132.70188903808594, 144.86705017089844], [0.0914628803730011, 3.0, 131.63890075683594, 96.77803802490234], [0.08678483217954636, 3.0, 398.6473693847656, 167.15406799316406], [0.08614408224821091, 3.0, 66.86555480957031, 144.81338500976562], [0.08607488870620728, 3.0, 691.43115234375, 247.60707092285156]], '4': [[0.7770470380783081, 4.0, 901.3283081054688, 68.18753051757812], [0.7728676199913025, 4.0, 801.3771362304688, 25.58776092529297], [0.17040516436100006, 4.0, 0.0, 0.3077890872955322], [0.1625441312789917, 4.0, 797.7902221679688, 16.972185134887695], [0.14011123776435852, 4.0, 67.15483856201172, 96.97804260253906], [0.12517213821411133, 4.0, 913.6453857421875, 73.39083099365234], [0.12231238186359406, 4.0, 1006.3551025390625, 379.1781005859375], [0.09073691815137863, 4.0, 335.1124572753906, 154.9727325439453], [0.08910531550645828, 4.0, 513.445556640625, 187.90850830078125], [0.08277841657400131, 4.0, 246.08944702148438, 117.5040512084961], [0.08205003291368484, 4.0, 997.8601684570312, 371.9066467285156], [0.08067314326763153, 4.0, 691.4340209960938, 229.71414184570312], [0.07862133532762527, 4.0, 602.7530517578125, 213.71917724609375], [0.07785378396511078, 4.0, 869.4149169921875, 264.6464538574219], [0.07703420519828796, 4.0, 846.0322875976562, 379.9542541503906]], '5': [], '6': [[0.21324686706066132, 6.0, 801.3771362304688, 25.58776092529297], [0.19707146286964417, 6.0, 901.3283081054688, 68.18753051757812], [0.16298823058605194, 6.0, 794.3505249023438, 16.831850051879883], [0.15589839220046997, 6.0, 67.15483856201172, 96.97804260253906], [0.15379877388477325, 6.0, 910.1263427734375, 76.38924407958984], [0.10051369667053223, 6.0, 246.08944702148438, 117.5040512084961], [0.09006985276937485, 6.0, 6.965068817138672, 5.26966667175293], [0.08492999523878098, 6.0, 156.06536865234375, 113.29442596435547]], '7': [[0.16100502014160156, 7.0, 678.4096069335938, 328.41827392578125], [0.09552208334207535, 7.0, 310.43646240234375, 344.4073181152344], [0.09357292205095291, 7.0, 845.8665771484375, 344.40545654296875]], '8': []}, {'1': [[0.5727570056915283, 1.0, 367.1124572753906, 161.33042907714844], [0.5294477343559265, 1.0, 277.7322082519531, 132.55072021484375], [0.5275318026542664, 1.0, 99.55134582519531, 120.75664520263672], [0.527033269405365, 1.0, 814.1870727539062, 260.23046875], [0.49405190348625183, 1.0, 546.1890869140625, 193.660400390625], [0.49243971705436707, 1.0, 277.8805847167969, 223.67822265625], [0.491975873708725, 1.0, 635.0733642578125, 219.59884643554688], [0.48197659850120544, 1.0, 367.12054443359375, 233.21041870117188], [0.45803746581077576, 1.0, 455.8502502441406, 179.54754638671875], [0.45402178168296814, 1.0, 455.9441833496094, 247.92214965820312], [0.45270296931266785, 1.0, 902.1717529296875, 285.4866943359375], [0.4480424225330353, 1.0, 99.77039337158203, 224.02114868164062], [0.4432659149169922, 1.0, 545.919189453125, 249.08349609375], [0.41983160376548767, 1.0, 188.23385620117188, 160.1438751220703], [0.3902136981487274, 1.0, 635.35986328125, 263.1542053222656], [0.36180996894836426, 1.0, 189.84930419921875, 253.4646453857422], [0.361054927110672, 1.0, 724.8920288085938, 238.84031677246094], [0.3563321828842163, 1.0, 902.5628051757812, 266.30230712890625], [0.3269451856613159, 1.0, 725.3831787109375, 275.21319580078125], [0.3092292547225952, 1.0, 813.8844604492188, 281.6246032714844], [0.1621534824371338, 1.0, 143.62399291992188, 196.2484893798828], [0.10586610436439514, 1.0, 188.08474731445312, 212.3465576171875], [0.08630374819040298, 1.0, 849.7064208984375, 45.40337371826172], [0.08600439876317978, 1.0, 635.2618408203125, 247.96986389160156], [0.08359388262033463, 1.0, 455.4322814941406, 165.02615356445312], [0.08015365153551102, 1.0, 323.1526794433594, 220.2833251953125], [0.0751451924443245, 1.0, 190.17425537109375, 120.46128845214844], [0.07224868983030319, 1.0, 234.35670471191406, 200.4617462158203], [0.07184059917926788, 1.0, 99.64362335205078, 108.54753875732422], [0.07030646502971649, 1.0, 278.4927062988281, 119.8756103515625], [0.06779282540082932, 1.0, 411.6544189453125, 227.90237426757812], [0.06624854356050491, 1.0, 100.1376724243164, 97.16805267333984], [0.0634787306189537, 1.0, 590.6552124023438, 240.1620330810547], [0.059854257851839066, 1.0, 834.5106201171875, 259.6585388183594], [0.058815453201532364, 1.0, 27.533180236816406, 160.6699981689453], [0.052461888641119, 1.0, 99.67720794677734, 152.60459899902344], [0.0469198077917099, 1.0, 278.27685546875, 156.28741455078125], [0.045700911432504654, 1.0, 189.9074249267578, 196.33265686035156], [0.04562670737504959, 1.0, 635.1829223632812, 232.33230590820312], [0.04150472208857536, 1.0, 322.7572937011719, 192.23277282714844], [0.04096462205052376, 1.0, 58.77729797363281, 184.53282165527344]], '2': [[0.045503880828619, 2.0, 247.45094299316406, 116.9471435546875]], '3': [[0.12810637056827545, 3.0, 234.35670471191406, 200.4617462158203], [0.09023828059434891, 3.0, 188.23385620117188, 160.1438751220703], [0.07850027084350586, 3.0, 143.62399291992188, 196.2484893798828], [0.07676849514245987, 3.0, 189.9074249267578, 196.33265686035156], [0.07581555843353271, 3.0, 189.84930419921875, 253.4646453857422], [0.07450836151838303, 3.0, 189.7581024169922, 207.99899291992188], [0.07264667749404907, 3.0, 277.7322082519531, 132.55072021484375], [0.06959802657365799, 3.0, 724.9365844726562, 238.08432006835938], [0.06451977044343948, 3.0, 99.55134582519531, 120.75664520263672], [0.06110410392284393, 3.0, 635.0733642578125, 219.59884643554688], [0.057427145540714264, 3.0, 258.7549133300781, 216.53207397460938], [0.05707583576440811, 3.0, 234.23861694335938, 160.3834686279297], [0.05623169243335724, 3.0, 222.6890106201172, 204.4864959716797], [0.053273800760507584, 3.0, 187.7435302734375, 176.23214721679688], [0.05315972492098808, 3.0, 455.8502502441406, 179.54754638671875], [0.05303262546658516, 3.0, 546.1890869140625, 193.660400390625], [0.052072953432798386, 3.0, 246.83563232421875, 196.5137481689453], [0.047642361372709274, 3.0, 262.6881103515625, 196.47412109375], [0.04738732799887657, 3.0, 457.779052734375, 167.90687561035156], [0.04736722633242607, 3.0, 457.9751281738281, 200.40631103515625], [0.046341150999069214, 3.0, 274.9679260253906, 196.43978881835938], [0.045749492943286896, 3.0, 558.3635864257812, 192.9044952392578], [0.043974246829748154, 3.0, 302.6476135253906, 220.47940063476562], [0.043217260390520096, 3.0, 637.22509765625, 248.0642547607422], [0.04287131130695343, 3.0, 546.6450805664062, 204.25515747070312], [0.04269777983427048, 3.0, 275.3088684082031, 168.52291870117188], [0.042510855942964554, 3.0, 635.1829223632812, 232.33230590820312], [0.04128013923764229, 3.0, 723.2198486328125, 228.7128143310547]], '4': [[0.5923395752906799, 4.0, 849.7064208984375, 45.40337371826172], [0.05474881827831268, 4.0, 814.4122924804688, 45.61722183227539], [0.04123351722955704, 4.0, 786.28515625, 40.99400329589844]], '5': [[0.1467374712228775, 5.0, 27.621829986572266, 16.882598876953125], [0.0897073820233345, 5.0, 38.1749267578125, 53.053157806396484], [0.0761747658252716, 5.0, 46.408851623535156, 56.25762939453125], [0.07411322742700577, 5.0, 27.533180236816406, 160.6699981689453], [0.05626148357987404, 5.0, 46.89292907714844, 76.49158477783203], [0.05131756514310837, 5.0, 27.084983825683594, 88.94209289550781]], '6': [[0.09872821718454361, 6.0, 849.7064208984375, 45.40337371826172], [0.08768914639949799, 6.0, 27.621829986572266, 16.882598876953125], [0.08501233905553818, 6.0, 247.3475341796875, 108.85694122314453], [0.08032146096229553, 6.0, 247.5462188720703, 132.77069091796875], [0.05560757964849472, 6.0, 99.55134582519531, 120.75664520263672], [0.05298423394560814, 6.0, 278.959716796875, 108.42192077636719], [0.05033797025680542, 6.0, 276.7742004394531, 132.36061096191406], [0.04250774905085564, 6.0, 278.81951904296875, 117.13550567626953]], '7': [[0.0931193009018898, 7.0, 275.0997619628906, 335.8082275390625], [0.08544769883155823, 7.0, 758.454345703125, 335.7279357910156], [0.07558508962392807, 7.0, 302.82489013671875, 336.43756103515625], [0.06984265148639679, 7.0, 690.5375366210938, 335.61297607421875], [0.06474475562572479, 7.0, 131.54147338867188, 335.6650695800781], [0.0631425529718399, 7.0, 99.2160873413086, 335.9851379394531], [0.062039464712142944, 7.0, 838.3231201171875, 336.255126953125], [0.061727795749902725, 7.0, 254.799072265625, 335.75567626953125], [0.060610082000494, 7.0, 790.6275024414062, 335.27569580078125], [0.05963662639260292, 7.0, 718.5501098632812, 335.96881103515625], [0.05740964412689209, 7.0, 67.20588684082031, 335.4141845703125], [0.052634645253419876, 7.0, 706.4454956054688, 335.8002624511719], [0.04515068605542183, 7.0, 846.345703125, 56.904869079589844]], '8': []}, [[367.1124572753906, 161.33042907714844, 335.46820068359375, 166.95306396484375, 335.1124572753906, 154.9727325439453, 0.99607253074646]]]\n",
      "(array([[802.,  25., 901.,  68.],\n",
      "       [ 28.,  23.,  47.,  91.],\n",
      "       [ 67., 145., 131., 304.],\n",
      "       [156., 205., 220., 304.],\n",
      "       [245., 147., 309., 304.],\n",
      "       [335., 167., 399., 304.],\n",
      "       [424., 194., 488., 304.],\n",
      "       [513., 199., 577., 304.],\n",
      "       [603., 224., 667., 304.],\n",
      "       [692., 248., 756., 304.],\n",
      "       [781., 263., 845., 304.],\n",
      "       [870., 267., 934., 304.],\n",
      "       [ 67.,  97., 131., 145.],\n",
      "       [156., 114., 220., 205.],\n",
      "       [245., 118., 309., 147.],\n",
      "       [335., 155., 399., 167.],\n",
      "       [424., 165., 488., 194.],\n",
      "       [513., 188., 577., 199.],\n",
      "       [603., 214., 667., 223.],\n",
      "       [692., 230., 756., 248.],\n",
      "       [781., 257., 845., 264.]]), array([4., 5., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,\n",
      "       1., 1., 1., 1.]))\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'chartType' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[12], line 17\u001B[0m\n\u001B[1;32m     15\u001B[0m \u001B[39mif\u001B[39;00m gts \u001B[39mis\u001B[39;00m \u001B[39mNone\u001B[39;00m \u001B[39mor\u001B[39;00m \u001B[39mlen\u001B[39m(gts) \u001B[39m==\u001B[39m \u001B[39m0\u001B[39m: \u001B[39mcontinue\u001B[39;00m\n\u001B[1;32m     16\u001B[0m \u001B[39mprint\u001B[39m(gts)\n\u001B[0;32m---> 17\u001B[0m gts \u001B[39m=\u001B[39m gts[\u001B[39m0\u001B[39m] \u001B[39mif\u001B[39;00m chartType \u001B[39m==\u001B[39m \u001B[39m'\u001B[39m\u001B[39mLine\u001B[39m\u001B[39m'\u001B[39m \u001B[39melse\u001B[39;00m gts\n\u001B[1;32m     18\u001B[0m \u001B[39mif\u001B[39;00m \u001B[39mlen\u001B[39m(gts) \u001B[39m==\u001B[39m \u001B[39m0\u001B[39m: \u001B[39mcontinue\u001B[39;00m\n\u001B[1;32m     19\u001B[0m gt_groups, pred_groups \u001B[39m=\u001B[39m get_grouped_points(gts, preds, chartType)\n",
      "\u001B[0;31mNameError\u001B[0m: name 'chartType' is not defined"
     ]
    }
   ],
   "source": [
    "# compute 0.75\n",
    "macro_P = []\n",
    "macro_R = []\n",
    "max_iter = db.db_inds.size\n",
    "\n",
    "for i in tqdm(range(max_iter)):\n",
    "    chartType = None\n",
    "    db_ind = db.db_inds[i]\n",
    "    image_file = db.image_file(db_ind)\n",
    "    gts = db.detections(db_ind)\n",
    "    preds = prediction[image_file.split('/')[-1]]\n",
    "    if preds is None or len(preds) == 0: continue\n",
    "    if len(preds) == 3 and len(preds[2]) == 0: continue\n",
    "    \n",
    "    print(preds)\n",
    "    if gts is None or len(gts) == 0: continue\n",
    "    print(gts)\n",
    "    gts = gts[0] if chartType == 'line' else gts\n",
    "    if len(gts) == 0: continue\n",
    "    gt_groups, pred_groups = get_grouped_points(gts, preds, chartType)\n",
    "\n",
    "    P = computePrecision(gt_groups, pred_groups, thres=0.75)\n",
    "    R = computeRecall(gt_groups, pred_groups, thres=0.75)\n",
    "    \n",
    "    macro_P.append(P)\n",
    "    macro_R.append(R)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "macro_P: 0.5838068181818181  macro_R: 0.5011583011583012 F score: 0.5393346347992855\n"
     ]
    }
   ],
   "source": [
    "macro_P = np.array(macro_P)\n",
    "macro_R = np.array(macro_R)\n",
    "macro_P_avg = macro_P[~np.isnan(macro_P)].mean()\n",
    "macro_R_avg = macro_R[~np.isnan(macro_R)].mean()\n",
    "print('macro_P:', macro_P_avg, \" macro_R:\", macro_R_avg, 'F score:', (2*macro_P_avg*macro_R_avg) / (macro_P_avg + macro_R_avg))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ChartReader",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
